// Local
#include "testmisc.h"
#include "misc.h"
#include "weight.h"
#include "volume.h"

// QtCore
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QString>

// QtXml
#include <QDomDocument>

// QtTest
#include <QTest>

// Entry point
QTEST_MAIN(testMisc)

void testMisc::Basics()
{
    QString Name = "Sang de serpent";
    int Version = 3098;
    MiscType Type = MTOther;
    MiscUse Use = MUBottling;
    Duration Time = Duration(6,Day);
    Weight Amount = Weight(6,Pound);
    Misc zeMisc(Name,Version,Type,Use,Time,Amount);

    QVERIFY(zeMisc._Name==Name);
    QVERIFY(zeMisc._Version==Version);
    QVERIFY(zeMisc._Type==Type);
    QVERIFY(zeMisc._Use==Use);
    QVERIFY(zeMisc._Time==Time);
    QVERIFY(zeMisc._AmountIsWeight==true);
    QVERIFY(zeMisc._AmountV==0.0);
    QVERIFY(zeMisc._AmountW==Amount);

    Volume Skull = Volume(124.78,Tablespoon);
    Misc zeMisc2(Name,Version,Type,Use,Time,Skull);    
    QVERIFY(zeMisc2._Name==Name);
    QVERIFY(zeMisc2._Version==Version);
    QVERIFY(zeMisc2._Type==Type);
    QVERIFY(zeMisc2._Use==Use);
    QVERIFY(zeMisc2._Time==Time);
    QVERIFY(zeMisc2._AmountIsWeight==false);
    QVERIFY(zeMisc2._AmountV==Skull);
    QVERIFY(zeMisc2._AmountW==0.0);

    QVERIFY(zeMisc2!=zeMisc);
}

void testMisc::ParseXML()
{
    Q_INIT_RESOURCE(test_misc);

    // Parse XML
    QFile Input(":/coriander.xml");
    QDomDocument doc("coriander");
    QVERIFY(Input.open(QIODevice::ReadOnly));
    QVERIFY(doc.setContent(&Input));

    Misc zeMisc;
    QVERIFY(0==zeMisc.CreateFromXMLNode(doc));
    QVERIFY(zeMisc._Name=="Coriander");
    QVERIFY(zeMisc._Version==1);
    QVERIFY(zeMisc._Use==MUBoil);
    QVERIFY(zeMisc._Type==MTSpice);
    QVERIFY(zeMisc._Time==Duration(5.0,Minute));
    QVERIFY(zeMisc._AmountIsWeight==false);
    QVERIFY(zeMisc._AmountV==0.025);
    QVERIFY(zeMisc._UseFor=="Belgian Wit Spice");
    QVERIFY(zeMisc._Notes=="Used in Belgian Wit, Whites, and Holiday ales.  Very good when used in light wheat ales.  Often used with Bitter Orange Peel. Crack open seeds and add at the end of the boil to extract aroma and flavor.");
}

void testMisc::ToXML()
{
    QString Name = "Snake blood";
    int Version = 3098;
    MiscType Type = MTOther;
    MiscUse Use = MUBottling;
    Duration Time = Duration(6,Day);
    Weight Amount = Weight(6,Pound);
    Misc zeMisc(Name,Version,Type,Use,Time,Amount);
    zeMisc._Notes = "Awful cursed ingredient, for good poisons.";
    zeMisc._UseFor = "Murder";

    QString zeString;
    QTextStream Stream(&zeString);
    QDomDocument docWrite("beerxml");
    zeMisc.ToXMLNode(docWrite);
    docWrite.save(Stream,2);
    qDebug() << "Dumping generated XML" << endl << zeString;

    QDomDocument docRead("beerxml");
    QVERIFY(docRead.setContent(zeString));
    Misc parsedMisc;
    QVERIFY(0==parsedMisc.CreateFromXMLNode(docRead));
    QVERIFY(zeMisc==parsedMisc);
}
